package com.szholly.plug.safe.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.session.SessionFactory;
import com.szholly.utils.session.provider.ISessionProvider;
import com.szholly.utils.spring.springmvc.route.FunctionInfo;
import com.szholly.plug.safe.entity.user.UserEntity;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component("directive_userdw")
public class CurUserDwDirective implements TemplateDirectiveModel, IDirective {
	@SuppressWarnings({ "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		UserEntity userName = null;
		ISessionProvider session = SessionFactory.getSession();
		if (session != null) {
			userName = (UserEntity) session
					.getObject(ISessionProvider.UserEntity);
		}
		String userNameString = "";
		if (userName != null) {
			userNameString = userName.getDW();
		}

		Writer out = env.getOut();
		out.append(userNameString);
	}

	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "" + "示例：<@userdw  />";
		FunctionInfo info = new FunctionInfo("userdw", 1, "ͨ基础", "当前登录用户的单位",
				paraInfo);
		return info;
	}
}
